package com.bdqn.t382.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.t382.entity.SysUser;
import com.bdqn.t382.service.SysUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
public class SysUserController {

    @Resource
    SysUserService sysUserService;
    @Resource
    private VehicleComplaintsController vehicleComplaintsController;

    @RequestMapping("/page")
    public String page(Model model,
                       @RequestParam(value = "current", defaultValue = "1") Integer current,
                       @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Page page = sysUserService.selectPage(current, size);
        model.addAttribute("page", page);
        return vehicleComplaintsController.selectAllByName(null, model);
    }

    @RequestMapping("/selectById")
    public String selectById(Model model, Integer id) {
        SysUser sysUser = sysUserService.selectById(id);
        model.addAttribute("sysUser", sysUser);
        return "view";
    }

    @RequestMapping("/selectByName")
    public String selectByName(Model model, String name) {
        SysUser sysUser = sysUserService.selectByName(name);
        model.addAttribute("sysUser", sysUser);
        return "view";
    }

    @RequestMapping("/deleteById")
    public String deleteById(Model model, Integer id) {
        int i = sysUserService.deleteById(id);
        return page(model, 1, 3);
    }

    @RequestMapping("/updateById")
    public String updateById(Model model, SysUser sysUser) {
        int i = sysUserService.updateById(sysUser);
        return page(model, 1, 3);
    }

    @RequestMapping("/add")
    public String add() {
        return "add";
    }

    @RequestMapping("/insert")
    public String insert(Model model, SysUser sysUser) {
        int i = sysUserService.insert(sysUser);
        return page(model, 1, 3);
    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @RequestMapping("/doLogin")
    public String doLogin(Model model, SysUser sysUser, HttpSession session) {
        SysUser user = null;
        if (sysUser.getAccount().length() < 1 || sysUser.getAccount() == null) {
            session.setAttribute("user", null);
            return "error";
        } else {
            user = sysUserService.login(sysUser.getAccount(), sysUser.getPassword());
            if (user == null) {
                session.setAttribute("user", null);
                return "error";
            } else if (!user.getPassword().equals(sysUser.getPassword())) {
                session.setAttribute("user", null);
                return "error";
            } else {
                session.setAttribute("user", user);
                return page(model, 1, 3);
            }
        }
    }
}